"""
Redis客户端配置
"""

import redis.asyncio as redis

from config.logging_config import get_logger
from constant.settings import settings

logger = get_logger(__name__)

# Redis客户端实例
redis_client = None


async def init_redis():
    """初始化Redis连接"""
    global redis_client
    try:
        redis_client = redis.from_url(
            settings.REDIS_URL,
            encoding="utf-8",
            decode_responses=True
        )
        # 测试连接
        await redis_client.ping()
        logger.info("Redis连接成功")
    except Exception as e:
        logger.error(f"Redis连接失败: {e}")
        raise


async def get_redis():
    """获取Redis客户端"""
    return redis_client


async def close_redis():
    """关闭Redis连接"""
    if redis_client:
        await redis_client.close()
